#-*- coding: utf-8 -*-，
#coding = utf-8

'''
Created on 2012-5-8

@author: songchen
'''
import MySQLdb
import pymongo

def getMysqlDate(port):
    conn = MySQLdb.connect(host="10.59.95.191",user="my_db",passwd="gQNszcP3wxNqUF4RMQXx",db="my_db",port=port,charset="utf8")
    cursor=conn.cursor()
    cursor.execute("""
            SELECT
             USER_ACCOUNT.USER_ID,
             USER_ACCOUNT.ACC_ID,
             USER_ACCOUNT.NICK_NAME,
             USER_ACCOUNT.EMAIL,
             USER_CONTACT.PROVINCE,
             USER_CONTACT.CITY,
             USER_GAME_NOW.GAME_ID,
             USER_GAME_NOW.GAME_NO,
             USER_GAME_NOW.GAME_NAME,
             USER_GAME_NOW.DOMAIN,
             USER_GAME_NOW.SERVER,
             USER_GAME_NOW.PARTY,
             USER_GAME_NOW.PLAYER_NICK,
             USER_GAME_NOW.PLAYER_ORG
            from USER_ACCOUNT Left JOIN USER_CONTACT ON USER_ACCOUNT.USER_ID=USER_CONTACT.USER_ID 
            Left JOIN USER_GAME_NOW ON USER_ACCOUNT.USER_ID=USER_GAME_NOW.UID
    """)
    res = cursor.fetchall()
    cursor.close()
    conn.close()
    return res

def toObj(data):
    obj = {}
    obj["uid"]=data[0]
    obj["accId"]=data[1]
    obj["nickname"]=data[2]
    obj["email"]=data[3]
    obj["province"]=data[4]
    obj["city"]=data[5]
    obj["gameId"]=data[6]
    obj["gameNo"]=data[7]
    obj["gameName"]=data[8]
    obj["domain"]=data[9]
    obj["server"]=data[10]
    obj["party"]=data[11]
    obj["playerNick"]=data[12]
    obj["playerOrg"]=data[13]
    return obj
    
def importToMongo():
    print "import data start ...."
    
    print "get mysql data." 
    res = getMysqlDate(3306)
    
    print "get mongodb conneciton..."
    userTable = getUserTable()
    
    for data in res:
        obj = toObj(data)
        print obj 
        userTable.save(obj)
    print "end"
            
def getUserTable():
    con = pymongo.Connection(host='127.0.0.1',port=27017)
    mydb = con.sns173
    return mydb.TempUser
    
    
def main():
    pass

if __name__=="__main__":
    importToMongo()
    
    
    
    
    
    
    
    
    
    
    
    
    